package com.polaris.live.utils.sensor.module

import androidx.appcompat.app.AppCompatActivity
import com.polaris.live.utils.sensor.base.DialogModule
import com.polaris.live.utils.sensor.base.FunctionModule
import com.polaris.live.utils.sensor.base.IModule
import com.polaris.live.common.cacha.CacheUtil
import com.polaris.live.common.constant.ConfigType
import com.polaris.live.common.constant.ServerConfig
import com.polaris.live.common.util.ToastGravity
import com.lxj.xpopup.XPopup

/**
 * ServerModule
 *
 * @author Created by lucas on 2023/10/25 17:46
 */
class ServerModule(
    context: AppCompatActivity
) : DialogModule(context) {

    override val title: String
        get() = "Switch config env[${ServerConfig.getServerUrl()}]"

    override fun createModules(): Array<IModule> = arrayOf(
        FunctionModule(mContext, "Switch to Prod", {
            CacheUtil.saveDebugEnv(ConfigType.PROD.value)

            showSuccessToast()
        }),
        FunctionModule(mContext, "Switch to Pre", {
            CacheUtil.saveDebugEnv(ConfigType.PRE.value)

            showSuccessToast()
        }),
        FunctionModule(mContext, "Switch to Test", {
            CacheUtil.saveDebugEnv(ConfigType.TEST.value)

            showSuccessToast()
        }),
        FunctionModule(mContext, "Switch to Custom", {
            XPopup.Builder(mContext)
                .hasStatusBarShadow(false)
                .isDestroyOnDismiss(true)
                .asInputConfirm("Custom Url", null, ServerConfig.getServerUrl(), null) {
                    ServerConfig.updateCustomConfig(it)

                    CacheUtil.saveDebugUrl(it)

                    showSuccessToast()
                }
                .show()
        })
    )

    private fun showSuccessToast() {
        ToastGravity.showGravity("Server config switched successfully, will take effect after restart")
    }
}